package lectura_escritura;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class GeneraXMLWidgets {
	
	private String entrada;
	private List<String> archivo;
	private String uri;
	private String tituloES;
	private String tituloEN;
	private String descCortaES;
	private String descCortaEN;
	private String descLargaES;
	private String descLargaEN;
	private String tagsES;
	private String tagsEN;
	
	public GeneraXMLWidgets(String tituloES, String tituloEN, String descCortaES, String descCortaEN, String descLargaES,
			String descLargaEN, String tagsES, String tagsEN, String uri){
		this.uri = uri;
		this.tituloES = tituloES;
		this.tituloEN = tituloEN;
		this.descCortaES = descCortaES;
		this.descCortaEN = descCortaEN;
		this.descLargaEN = descLargaEN;
		this.descLargaES = descLargaES;
		this.tagsES = tagsES;
		this.tagsEN = tagsEN;
		
		this.archivo = new ArrayList<String>();
	}
	
	
	public List<String> leeAsset(){
		try {
			InputStream lector = new FileInputStream("/home/javictoria/Escritorio/asset.xml");
			BufferedReader bf = new BufferedReader(new InputStreamReader(lector));
			
			
			while((entrada=bf.readLine())!=null){
				if(entrada.contains("@tites")){
					entrada = "\t\t"+tituloES;
				}
				else if(entrada.contains("@titen")){
					entrada = "\t\t"+tituloEN;
				}
				else if(entrada.contains("@cortoes")){
					entrada = "\t\t"+descCortaES;
				}
				else if(entrada.contains("@cortoen")){
					entrada = "\t\t"+descCortaEN;
				}
				else if(entrada.contains("@largoes")){
					entrada = "\t\t"+descLargaES;
				}
				else if(entrada.contains("@largoen")){
					entrada = "\t\t"+descLargaEN;
				}
				else if(entrada.contains("@tages")){
					entrada = "\t\t"+tagsES;
				}
				else if(entrada.contains("@tagen")){
					entrada = "\t\t"+tagsEN;
				}
				else if(entrada.contains("@uri")){
					entrada = "\t<item uri=\""+uri+"\" contentType=\"application/x-opera-widgets\" />";
				}
				archivo.add(entrada+"\n");
			}
			lector.close();
			bf.close();
				
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(IOException e){
	    	e.printStackTrace();
	    }
		
		return archivo;
	}
	
	public void generaXML(){
		FileWriter f;
		try {
			f = new FileWriter("/home/javictoria/Escritorio/"+tituloEN+"_asset.xml");
			for(String s:archivo)
				f.write(s);
			f.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
